package org.sixtynine.stock.controller;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.IntradaySectorData;
import org.sixtynine.stock.entity.Sector;
import org.sixtynine.stock.model.LoadingSearchCriteria;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.IntradaySectorDataService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 *         this class use to add , edit and delete intraday sector data
 * 
 */

@Controller
@SessionAttributes
public class IntraDaySectorController {

	private int RECORDS_PER_PAGE = 10;

	@Autowired
	private GenericService genericService;

	@Autowired
	DataManagerService dataManagerService;

	@Autowired
	IntradaySectorDataService intradaySectorDataService;

	@RequestMapping(value = "/admin/intradaysectordata/search")
	public ModelAndView search(@ModelAttribute LoadingSearchCriteria criteria) {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/list");
		loadData(1, -1, criteria.getSectorId(), criteria.getFromDate(),
				criteria.getToDate(), modelAndView, criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/intradaysectordata/add")
	public ModelAndView addUser() {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/add");
		modelAndView.addObject("intradaysectordata", new IntradaySectorData());

		List<BaseEntity> sector = genericService.findAll(Sector.class);
		modelAndView.addObject("sectorMap", sector);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/intradaysectordata/add/process")
	public ModelAndView addingUser(
			@ModelAttribute @Valid IntradaySectorData intradaySectorData,
			BindingResult result) {

		/* if (!result.hasErrors()) { */
		genericService.saveOrUpdate(intradaySectorData);
		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/list");
		List<BaseEntity> intradaySectorDataList = genericService
				.findAll(IntradaySectorData.class);
		modelAndView
				.addObject("intradaySectorDataList", intradaySectorDataList);

		return modelAndView;
		/*
		 * } else { ModelAndView modelAndView = new
		 * ModelAndView("intradaysectordata/add"); List<BaseEntity> sector =
		 * genericService.findAll(Sector.class);
		 * modelAndView.addObject("sectorMap", sector); return modelAndView; }
		 */

	}

	@RequestMapping(value = "/admin/intradaysectordata/list")
	public ModelAndView listOfTeams(
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "totalRecords", required = false) Integer totalRecords) {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/list");
		loadData(page, totalRecords, -1, null, null, modelAndView, null);

		return modelAndView;
	}

	/**
	 * load data
	 * 
	 * @param page
	 * @param totalRecords
	 * @param companyId
	 * @param fromDate
	 * @param toDate
	 * @param modelAndView
	 */
	private void loadData(Integer page, Integer totalRecords, int sectorId,
			Date fromDate, Date toDate, ModelAndView modelAndView,
			LoadingSearchCriteria criteria) {
		GridPramDTO gridPramDTO = new GridPramDTO();
		gridPramDTO.setRows(RECORDS_PER_PAGE);
		if (page != null) {
			gridPramDTO.setPage(page);
		} else {
			gridPramDTO.setPage(1);
		}
		if (totalRecords != null) {
			gridPramDTO._setTotalNumberOfRecords(totalRecords);
		}

		Page<IntradaySectorData> intradaySectorDataPage = intradaySectorDataService
				.searchIntradaySectorData(sectorId, fromDate, toDate,
						gridPramDTO);
		modelAndView
				.addObject("intradaySectorDataPage", intradaySectorDataPage);

		List<BaseEntity> sectors = genericService.findAll(Sector.class);
		modelAndView.addObject("sectorsMap", sectors);
		if (criteria == null) {
			criteria = new LoadingSearchCriteria();

			criteria.setSectorId(sectorId);
			criteria.setFromDate(fromDate);
			criteria.setToDate(toDate);
		}
		modelAndView.addObject("criteria", criteria);

	}

	@RequestMapping(value = "/admin/intradaysectordata/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editTeamPage(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/edit");
		BaseEntity intradaySectorData = genericService.findById(id,
				IntradaySectorData.class);
		modelAndView.addObject("intradaySectorData", intradaySectorData);

		List<BaseEntity> sector = genericService.findAll(Sector.class);
		modelAndView.addObject("sectorMap", sector);

		return modelAndView;
	}

	@RequestMapping(value = "/admin/intradaysectordata/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingTeam(
			@ModelAttribute @Valid IntradaySectorData intradaySectorData,
			BindingResult result, @PathVariable Integer id) {

//		System.out.println(intradaySectorData.getDateTime());
		/* if (!result.hasErrors()) { */
		genericService.saveOrUpdate(intradaySectorData);

		ModelAndView modelAndView = new ModelAndView(
				"/admin/intradaysectordata/list");
		List<BaseEntity> intradaySectorDataList = genericService
				.findAll(IntradaySectorData.class);
		modelAndView
				.addObject("intradaySectorDataList", intradaySectorDataList);
		String message = "Team was successfully edited.";
		modelAndView.addObject("message", message);

		return modelAndView;
		/*
		 * } else{ ModelAndView modelAndView = new
		 * ModelAndView("/intradaysectordata/edit"); List<BaseEntity> sector =
		 * genericService.findAll(Sector.class);
		 * modelAndView.addObject("sectorMap", sector); return modelAndView;
		 * 
		 * }
		 */
	}

	@RequestMapping(value = "/admin/intradaysectordata/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteTeam(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("/intradaysectordata/list");
		List<BaseEntity> intradaySectorDataList = genericService
				.findAll(IntradaySectorData.class);
		modelAndView
				.addObject("intradaySectorDataList", intradaySectorDataList);
		genericService.delete(id, IntradaySectorData.class);
		String message = "Team was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}

}
